import {assert} from "../../utils/assert";

class BasicManager {

    constructor() {
        this.collection = {};
    }

    add(name, item) {
        assert(item === undefined, "manage item can't NULL!");
        if (this.collection[name] === undefined) {
            this.collection[name] = item;
        }
    }

    get(name) {
        if (this.collection[name]) {
            return this.collection[name];
        }
        return undefined;
    }

    exists(name) {
        return !(this.collection[name] === undefined);
    }

    remove(name) {
        if (this.collection[name]) {
            this.beforeRemove(this.collection[name]);
            this.collection[name] = null;
            delete this.collection[name];
            this.afterRemove();
        }
    }

    release() {
        for (let key in this.collection) {
            delete this.collection[key];
        }
        this.collection = null;
    }

    each(fn) {
        if (typeof fn !== 'function') {
            return;
        }
        for (let key in this.collection) {
            fn.call(this, key, this.collection[key]);
        }
    }

    beforeRemove(item) {
    }

    afterRemove() {
    }
}

export {BasicManager};